﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.BL;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Module.Model.CMS;
using Project.Engine.Utilities;

namespace Project.Module.DAO.CMSBL
{
    public class UserBL : IBLTable<Employee, int>
    {
        public bool CMSLoginUser(string name, string pass)
        {
            pass = SecurityUtil.MD5Encrypt(pass);
            var query = from e in this.Context.Employees
                        where e.UserName == name && e.Password == pass
                        select new
                        {
                            Id = e.EmployeeID.ToString(),
                            Username = e.UserName,
                        };

            if (query != null && query.ToList().Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool CMSCheckAccountUser(string name)
        {
            var query = from e in this.Context.Employees
                        where e.UserName == name
                        select e.UserName;

            if (query != null && query.ToList().Count >= 1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public void CMSInsertUser(string name, string email, string pass)
        {
            int id = CMSGetMaxId() + 1;
            DateTime date = DateTime.Now;
            pass = SecurityUtil.MD5Encrypt(pass);

            string sql = @"insert into Employee (
                              EmployeeID, UserName, Password, Email, JoinedDate, LastTimeLogIn
                           ) values (
                              {0}, {1}, {2}, {3}, {4}, {5}
                           )";
            var query = this.Context.ExecuteQuery<CustomerModel>(sql, new object[] { id, name, pass, email, date, date }).ToList();
        }

        public void CMSUpdateUser(int id, string email, string pass)
        {
            DateTime date = DateTime.Now;

            if (String.IsNullOrEmpty(pass) == false)
            {
                pass = SecurityUtil.MD5Encrypt(pass);

                string sql = @"update Employee
                               set Password = {0}, Email = {1}, JoinedDate = {2}, LastTimeLogIn = {3}
                               where EmployeeID = {4}";
                var query = this.Context.ExecuteQuery<CustomerModel>(sql, new object[] { pass, email, date, date, id }).ToList();
            }
            else
            {
                string sql = @"update Employee
                               set Email = {0}, JoinedDate = {1}, LastTimeLogIn = {2}
                               where EmployeeID = {3}";
                var query = this.Context.ExecuteQuery<CustomerModel>(sql, new object[] { email, date, date, id }).ToList();
            }
        }

        private int CMSGetMaxId()
        {
            int Id = 0;
            var query = from e in this.Context.Employees
                        orderby e.EmployeeID descending
                        select new { Id = e.EmployeeID };

            if (query != null)
            {
                Id = ConvertUtil.ToInt32(query.Select(c => c.Id).FirstOrDefault().ToString());
            }

            // return
            return Id;
        }

        /// <summary>
        /// get all user in table Employee
        /// exception: account Admin
        /// </summary>
        /// <returns></returns>
        public List<EmployeeModel> GetAllUser()
        {
            var query = from e in this.Context.Employees
                        where e.UserName.ToLower().Contains("admin") == false
                        select new EmployeeModel()
                        {
                            EmployeeID = e.EmployeeID,
                            username = e.UserName,
                            firstname = e.FirstName,
                            lastname = e.LastName,
                            position = e.Title,
                            address = e.Address,
                            phone = (Int32)e.PhoneNumber,
                            mail = e.Email,
                            sex = e.Sex,
                            JoinDate = e.JoinedDate != null ? e.JoinedDate : Convert.ToDateTime("01/01/1900"),
                        };

            if (query != null && query.ToList().Count > 0)
            {
                return query.ToList();
            }
            //return
            return null;
        }

        public EmployeeModel GetDetailUser(int employeeID)
        {
            var query = from e in this.Context.Employees
                        where e.UserName.ToLower().Contains("admin") == false && e.EmployeeID == employeeID
                        select new EmployeeModel()
                        {
                            EmployeeID = e.EmployeeID,
                            username = e.UserName,
                            mail = e.Email,
                            JoinDate = e.JoinedDate != null ? e.JoinedDate : Convert.ToDateTime("01/01/1900"),
                        };

            if (query != null && query.ToList().Count > 0)
            {
                return query.ToList().FirstOrDefault();
            }
            //return
            return null;
        }

        public void DeleteUser(int id)
        {
            Employee employee = this.Context.Employees.Single(c => c.EmployeeID == id && c.UserName.ToLower().Contains("admin") == false);
            this.Context.Employees.DeleteOnSubmit(employee);
            this.Context.SubmitChanges();
        }

        public List<EmployeeModel> Search(string name, string email)
        {
            if (name.ToLower().Contains("admin") == true || String.IsNullOrEmpty(name) == true)
            {
                return null;
            }
            else
            {
                var query = from e in this.Context.Employees
                            where (name != String.Empty && e.UserName.Contains(name) == true) ||
                            (email != String.Empty && e.Email.Contains(email) == true)
                            select new EmployeeModel()
                            {
                                EmployeeID = e.EmployeeID,
                                username = e.UserName,
                                mail = e.Email,
                                JoinDate = e.JoinedDate != null ? e.JoinedDate : Convert.ToDateTime("01/01/1900"),
                            };

                if (query != null && query.ToList().Count > 0)
                {
                    return query.Where(c => c.username.Contains("admin") == false).ToList();
                }
                //return
                return null;
            }
        }

        public List<EmployeeModel> SearchUser(string name)
        {
            if (name.ToLower().Contains("admin") == true || String.IsNullOrEmpty(name) == true)
            {
                return null;
            }
            else
            {
                var query = from e in this.Context.Employees
                            where e.UserName.Contains(name) == true
                            select new EmployeeModel()
                            {
                                EmployeeID = e.EmployeeID,
                                username = e.UserName,
                                mail = e.Email,
                                JoinDate = e.JoinedDate != null ? e.JoinedDate : Convert.ToDateTime("01/01/1900"),
                            };

                if (query != null && query.ToList().Count > 0)
                {
                    return query.Where(c => c.username.Contains("admin") == false).ToList();
                }
                //return
                return null;
            }
        }

        public List<EmployeeModel> SearchUserByEmail(string mail)
        {
            var query = from e in this.Context.Employees
                        where e.Email.ToLower().Contains(mail) == true
                        select new EmployeeModel()
                        {
                            EmployeeID = e.EmployeeID,
                            username = e.UserName,
                            mail = e.Email,
                            JoinDate = e.JoinedDate != null ? e.JoinedDate : Convert.ToDateTime("01/01/1900"),
                        };

            if (query != null && query.ToList().Count > 0)
            {
                return query.Where(c => c.username.Contains("admin") == false).ToList();
            }
            //return
            return null;
        }
    }
}
